//
//  RootViewController.m
//  UI8_Contact_Arr
//
//  Created by TravelRound on 15/11/18.
//  Copyright © 2015年 TravelRound. All rights reserved.
//

#import "RootViewController.h"
#import "Student.h"
#import "DetailViewController.h"

@interface RootViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) NSMutableDictionary *stuDic;

@end

@implementation RootViewController

- (void)dealloc
{
    [_stuDic release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"通讯录";
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"StudentInfo" ofType:@"plist"];
    
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
    
    self.stuDic = [NSMutableDictionary dictionary];
    
    for (NSString *key in dic) {
        
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *tempDic in [dic objectForKey:key]) {
            
            Student *stu = [[Student alloc] init];
            stu.name = [tempDic objectForKey:@"name"];
            stu.phone = [tempDic objectForKey:@"phone"];
            // 将学生信息添加到数组
            [arr addObject:stu];
            [stu release];
            
        }
        // 将学生数组添加到字典
        [self.stuDic setObject:arr forKey:key];
        
    }
    
    UITableView *tableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    tableV.delegate = self;
    tableV.dataSource = self;
    // 去除分割线
    tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableV];
    [tableV release];
    
}

// 选中某一行时触发
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 获取点击的同学信息以便传递给详情界面
    NSString *key = [self.stuDic.allKeys objectAtIndex:indexPath.section];
    NSMutableArray *arr = [self.stuDic objectForKey:key];
    Student *stu = [arr objectAtIndex:indexPath.row];
    
    // 创建详情界面以便切换
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    // 属性传值, 将对应的同学信息进行传递
//    detailVC.nameTitle = stu.name;
    detailVC.stu = stu;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
    [detailVC release];
}

// 分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // 每一个key对应一个区
    return self.stuDic.allKeys.count;
}

// 每个区的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    // 当前区显示对应的key
    return [self.stuDic.allKeys objectAtIndex:section];
}

//右侧竖排索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.stuDic.allKeys;
}

// 每个区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // 根据区号得到key
    NSString *key = [self.stuDic.allKeys objectAtIndex:section];
    // 根据key得到对应的数组
    NSMutableArray *arr = [self.stuDic objectForKey:key];
    // 返回当前区对应的数组个数,即当前区的行数
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (nil == cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr] autorelease];
    }
    
    // 去除选中阴影
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    // 根据区号获取到key
    NSString *key = [self.stuDic.allKeys objectAtIndex:indexPath.section];
    // 根据key获取到数组
    NSMutableArray *arr = [self.stuDic objectForKey:key];
    // 根据行号获取到stu
    Student *stu = [arr objectAtIndex:indexPath.row];
    
    cell.textLabel.text = stu.name;
    cell.detailTextLabel.text = stu.phone;

    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
